Euclidean Algorithm
The Euclidean Algorithm is a procedure for finding the greatest common divisor of a pair of integers.
We will use the following result here throughout.
Theorem
The Euclidean Algorithm proceeds to calculate the greatest common divisor of two integers
Consider the greatest common divisor of
We find apply the division algorithm for integers to
We then repeatedly apply the division algorithm to the divisor and remainder:
Since
Implementation
let rec gcd a b =
// Division algorithm
let r = (a % b + b) % b
let q = (a - r) / b
if r <> 0 then
gcd b r
else
b
printfn "%d" (gcd 7 255)